﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;
using CoolERP_DAL;

namespace CoolERP_BLL
{
    public class KCBLL : BaseBLL<KC>
    {       
        public KCBLL() { }

        public List<WLKCModel> GetWLKC(Dictionary<string, string> dc, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var predicate = PredicateExtensions.True<WLKCModel>();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("WPLX") && !string.IsNullOrEmpty(dc["WPLX"]))
                    predicate = predicate.AndEx(t => t.WPLX == dc["WPLX"]);
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                    predicate = predicate.AndEx(t => t.BH.Contains(dc["BH"]));
                if (dc.ContainsKey("MC") && !string.IsNullOrEmpty(dc["MC"]))
                    predicate = predicate.AndEx(t => t.MC.Contains(dc["MC"]));
                if (dc.ContainsKey("WLLX") && !string.IsNullOrEmpty(dc["WLLX"]))
                    predicate = predicate.AndEx(t => t.LXBH == dc["WLLX"]);
                if (dc.ContainsKey("GYS") && !string.IsNullOrEmpty(dc["GYS"]))
                {
                    Guid id = new Guid(dc["GYS"]);
                    predicate = predicate.AndEx(t => t.GYSID == id);
                }
                if (dc.ContainsKey("WPID") && !string.IsNullOrEmpty(dc["WPID"]))
                {
                    string[] ids = dc["WPID"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    predicate = predicate.AndEx(t => ids.Contains(t.WPID.ToString()));
                }
                if (dc.ContainsKey("BHList") && !string.IsNullOrEmpty(dc["BHList"]))
                {
                    string[] bhs = dc["BHList"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    predicate = predicate.AndEx(t => bhs.Contains(t.BH.Trim()));
                }
                if (dc.ContainsKey("NotBHList") && !string.IsNullOrEmpty(dc["NotBHList"]))
                {
                    string[] bhs = dc["NotBHList"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    predicate = predicate.AndEx(t => !bhs.Contains(t.BH.Trim()));
                }
            }
            IKCDAL _dal = new KCDAL(context);
            return _dal.GetWLKC(predicate.Compile(), isDistinct, orderExpr, pageIndex, pageSize, out totalCount);
        }

        public List<BZXKCModel> GetBZXKC(Dictionary<string, string> dc, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var predicate = PredicateExtensions.True<BZXKCModel>();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("WPLX") && !string.IsNullOrEmpty(dc["WPLX"]))
                    predicate = predicate.AndEx(t => t.WPLX == dc["WPLX"]);
                if (dc.ContainsKey("cpxbh") && !string.IsNullOrEmpty(dc["cpxbh"]))
                    predicate = predicate.AndEx(t => t.CPXBH == dc["cpxbh"]);
                if (dc.ContainsKey("hxcbh") && !string.IsNullOrEmpty(dc["hxcbh"]))
                    predicate = predicate.AndEx(t => t.CPHXCBH == dc["hxcbh"]);
                if (dc.ContainsKey("bh") && !string.IsNullOrEmpty(dc["bh"]))
                    predicate = predicate.AndEx(t => t.BH.Contains(dc["bh"]));
                if (dc.ContainsKey("mc") && !string.IsNullOrEmpty(dc["mc"]))
                    predicate = predicate.AndEx(t => t.MC.Contains(dc["mc"]));
                if (dc.ContainsKey("lx") && !string.IsNullOrEmpty(dc["lx"]))
                {
                    int keyVal = Convert.ToInt32(dc["lx"]);
                    predicate = predicate.AndEx(t => t.LX == keyVal);
                }
                if (dc.ContainsKey("WPID") && !string.IsNullOrEmpty(dc["WPID"]))
                {
                    string[] ids = dc["WPID"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    predicate = predicate.AndEx(t => ids.Contains(t.WPID.ToString()));
                }
            }
            IKCDAL _dal = new KCDAL(context);
            return _dal.GetBZXKC(predicate.Compile(), isDistinct, orderExpr, pageIndex, pageSize, out totalCount);
        }

        public List<KCMXModel> GetKCMX(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var predicate = PredicateExtensions.True<KCMXModel>();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("WPID") && !string.IsNullOrEmpty(dc["WPID"]))
                {
                    Guid id = new Guid(dc["WPID"]);
                    predicate = predicate.AndEx(t => t.WPID == id);
                }
            }
            IKCDAL _dal = new KCDAL(context);
            return _dal.GetKCMX(predicate.Compile(), orderExpr, pageIndex, pageSize, out totalCount);
        }

        public double GetYKCSL(Guid id, string lx)
        {
            IKCDAL _dal = new KCDAL(context);
            return _dal.GetList(t => t.WPID == id && t.WPLX == lx).Sum(t => t.SL);
        }
    }
}
